#include <iostream>
#include <vector>
#include <queue>
using namespace std;

const int N = 100005;

int n, m;
vector<vector<int>> adl;
vector<int> length;
vector<bool> vis;
int ret;

void bfs(int u)
{
    queue<int> q; q.push(u);
    vis[u] = true;
    length[u] = 0;
    while (q.size())
    {
        auto t = q.front(); q.pop();
        for (auto e : adl[t])
        {
            if (!vis[e])
            {
                vis[e] = true;
                length[e] = length[t] + 1;
                q.push(e);
            }
        }
    }
}

int main()
{
    cin >> n >> m;

    adl.resize(n + 1);
    for (int i = 0; i < m; ++i)
    {
        int a, b; cin >> a >> b;
        adl[a].push_back(b);
    }

    length.resize(n + 1, -1);
    vis.resize(n + 1, false);

    bfs(1);

    cout << length[n] << endl;

    return 0;
}